package com.yc.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.yc.entity.Feedback;
import com.yc.mapper.FeedbackMapper;
import com.yc.service.FeedbackService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 投诉和建议表;(feedback)表服务实现类
 * @date : 2023-6-28
 */
@Service
public class FeedbackServiceImpl implements FeedbackService{
    @Autowired
    private FeedbackMapper feedbackMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Feedback queryById(Integer id){
        return feedbackMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param feedback 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<Feedback> paginQuery(Feedback feedback, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(feedback.getStuid())){
            queryWrapper.eq(Feedback::getStuid, feedback.getStuid());
        }
        if(StrUtil.isNotBlank(feedback.getDescription())){
            queryWrapper.eq(Feedback::getDescription, feedback.getDescription());
        }
        if(StrUtil.isNotBlank(feedback.getEvidence())){
            queryWrapper.eq(Feedback::getEvidence, feedback.getEvidence());
        }
        //2. 执行分页查询
        Page<Feedback> pagin = new Page<>(current , size , true);
        IPage<Feedback> selectResult = feedbackMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param feedback 实例对象
     * @return 实例对象
     */
    @Override
    public Feedback insert(Feedback feedback){
        feedbackMapper.insert(feedback);
        return feedback;
    }

    /**
     * 新增数据(测试：仅添加学号、内容、状态）
     *
     * @param  stuid
     * @param description
     * @param state
     */
    public void inserttest(String stuid, String description, int state){
        feedbackMapper.insertSuggesttest(stuid,description,state);
    }
    /**
     * 新增数据(正式：学号、内容、状态、文件）
     *
     * @param  stuid
     * @param description
     * @param state
     */

    public void insertAll(String stuid, String description, int state, String filepath){
        String id = feedbackMapper.selectidbystuid(stuid);
        feedbackMapper.insert(stuid,description,filepath,state,id);
    }

    /**
     * 新增已读状态
     *
     * @param id
     */
    public void insertreadornot(int id){
        feedbackMapper.insertreadornot(id);
    }

    /**
     * 查询学生姓名
     *
     * @return 学生姓名 投诉id list集合
     */
    public List<Map<String,Object>> selectstuname(){
        List<Map<String,Object>> stunamelist = new ArrayList<>();
        for (Map<String, Object> stuinfo : feedbackMapper.selectstuname()) {
            if (1 == (Integer)stuinfo.get("state")){
                stuinfo.put("sname", "匿名");
            }
            stunamelist.add(stuinfo);
        }
        return stunamelist;
    }

    /**
     * 用id查询投诉具体内容
     *
     * @param id
     * @return 投诉内容 证据路径
     */
    public Map<String,Object> selectfeedbacklist(int id){
        Map<String,Object> feedbackinfo = feedbackMapper.selectfeedbacklist(id);
        return feedbackinfo;
    }

    /**
     * 更新数据
     *
     * @param feedback 实例对象
     * @return 实例对象
     */
    @Override
    public Feedback update(Feedback feedback){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<Feedback> chainWrapper = new LambdaUpdateChainWrapper<Feedback>(feedbackMapper);
        if(StrUtil.isNotBlank(feedback.getStuid())){
            chainWrapper.eq(Feedback::getStuid, feedback.getStuid());
        }
        if(StrUtil.isNotBlank(feedback.getDescription())){
            chainWrapper.eq(Feedback::getDescription, feedback.getDescription());
        }
        if(StrUtil.isNotBlank(feedback.getEvidence())){
            chainWrapper.eq(Feedback::getEvidence, feedback.getEvidence());
        }
        //2. 设置主键，并更新
        chainWrapper.set(Feedback::getId, feedback.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(feedback.getId());
        }else{
            return feedback;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id){
        int total = feedbackMapper.deleteById(id);
        return total > 0;
    }
}
